﻿using System;
using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.LogNet;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 基于webapi的数据访问的基类，提供了基本的http接口的交互功能<br />
	/// A base class for data access based on webapi that provides basic HTTP interface interaction
	/// </summary>
	/// <remarks>
	/// 当前的基类在.net framework上存在问题，在.net framework4.5及.net standard上运行稳定而且正常
	/// </remarks>
	// Token: 0x02000141 RID: 321
	public class NetworkWebApiBase
	{
		/// <summary>
		/// 使用指定的ip地址来初始化对象<br />
		/// Initializes the object using the specified IP address
		/// </summary>
		/// <param name="ipAddress">Ip地址信息</param>
		// Token: 0x06001C27 RID: 7207 RVA: 0x0008B1F4 File Offset: 0x000893F4
		public NetworkWebApiBase(string ipAddress)
		{
			this.ipAddress = ipAddress;
			this.httpClient = new HttpClient();
		}

		/// <summary>
		/// 使用指定的ip地址及端口号来初始化对象<br />
		/// Initializes the object with the specified IP address and port number
		/// </summary>
		/// <param name="ipAddress">Ip地址信息</param>
		/// <param name="port">端口号信息</param>
		// Token: 0x06001C28 RID: 7208 RVA: 0x0008B244 File Offset: 0x00089444
		public NetworkWebApiBase(string ipAddress, int port)
		{
			this.ipAddress = ipAddress;
			this.port = port;
			this.httpClient = new HttpClient();
		}

		/// <summary>
		/// 使用指定的ip地址，端口号，用户名，密码来初始化对象<br />
		/// Initialize the object with the specified IP address, port number, username, and password
		/// </summary>
		/// <param name="ipAddress">Ip地址信息</param>
		/// <param name="port">端口号信息</param>
		/// <param name="name">用户名</param>
		/// <param name="password">密码</param>
		// Token: 0x06001C29 RID: 7209 RVA: 0x0008B29C File Offset: 0x0008949C
		public NetworkWebApiBase(string ipAddress, int port, string name, string password)
		{
			this.ipAddress = HslHelper.GetIpAddressFromInput(ipAddress);
			this.port = port;
			this.name = name;
			this.password = password;
			bool flag = !string.IsNullOrEmpty(name);
			if (flag)
			{
				HttpClientHandler httpClientHandler = new HttpClientHandler
				{
					Credentials = new NetworkCredential(name, password)
				};
				httpClientHandler.Proxy = null;
				httpClientHandler.UseProxy = false;
				this.httpClient = new HttpClient(httpClientHandler);
			}
			else
			{
				this.httpClient = new HttpClient();
			}
		}

		/// <summary>
		/// 等待重写的额外的指令信息的支持。除了url的形式之外，还支持基于命令的数据交互<br />
		/// Additional instruction information waiting for rewriting is supported.In addition to the url format, command based data interaction is supported
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>是否读取成功的内容</returns>
		// Token: 0x06001C2A RID: 7210 RVA: 0x0008B34B File Offset: 0x0008954B
		protected virtual OperateResult<string> ReadByAddress(string address)
		{
			return new OperateResult<string>(StringResources.Language.NotSupportedFunction);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkWebApiBase.ReadByAddress(System.String)" />
		// Token: 0x06001C2B RID: 7211 RVA: 0x0008B35C File Offset: 0x0008955C
		[DebuggerStepThrough]
		protected virtual Task<OperateResult<string>> ReadByAddressAsync(string address)
		{
			NetworkWebApiBase.<ReadByAddressAsync>d__4 <ReadByAddressAsync>d__ = new NetworkWebApiBase.<ReadByAddressAsync>d__4();
			<ReadByAddressAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadByAddressAsync>d__.<>4__this = this;
			<ReadByAddressAsync>d__.address = address;
			<ReadByAddressAsync>d__.<>1__state = -1;
			<ReadByAddressAsync>d__.<>t__builder.Start<NetworkWebApiBase.<ReadByAddressAsync>d__4>(ref <ReadByAddressAsync>d__);
			return <ReadByAddressAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 读取对方信息的的数据信息，通常是针对GET的方法信息设计的。如果使用了url=开头，就表示是使用了原生的地址访问<br />
		/// Read the other side of the data information, usually designed for the GET method information.If you start with url=, you are using native address access
		/// </summary>
		/// <param name="address">无效参数</param>
		/// <returns>带有成功标识的byte[]数组</returns>
		// Token: 0x06001C2C RID: 7212 RVA: 0x0008B3A8 File Offset: 0x000895A8
		public virtual OperateResult<byte[]> Read(string address)
		{
			OperateResult<string> operateResult = this.ReadString(address);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(Encoding.UTF8.GetBytes(operateResult.Content));
			}
			return result;
		}

		/// <summary>
		/// 读取对方信息的的字符串数据信息，通常是针对GET的方法信息设计的。如果使用了url=开头，就表示是使用了原生的地址访问<br />
		/// The string data information that reads the other party information, usually designed for the GET method information.If you start with url=, you are using native address access
		/// </summary>
		/// <param name="address">地址信息</param>
		/// <returns>带有成功标识的字符串数据</returns>
		// Token: 0x06001C2D RID: 7213 RVA: 0x0008B3F0 File Offset: 0x000895F0
		public virtual OperateResult<string> ReadString(string address)
		{
			bool flag = !Authorization.nzugaydgwadawdibbas();
			OperateResult<string> result;
			if (flag)
			{
				result = new OperateResult<string>(StringResources.Language.AuthorizationFailed);
			}
			else
			{
				bool flag2 = address.StartsWith("url=") || address.StartsWith("URL=");
				if (flag2)
				{
					address = address.Substring(4);
					string requestUri = string.Format("http://{0}:{1}/{2}", this.ipAddress, this.port, address.StartsWith("/") ? address.Substring(1) : address);
					try
					{
						using (HttpResponseMessage result2 = this.httpClient.GetAsync(requestUri).Result)
						{
							using (HttpContent content = result2.Content)
							{
								result2.EnsureSuccessStatusCode();
								string result3 = content.ReadAsStringAsync().Result;
								return OperateResult.CreateSuccessResult<string>(result3);
							}
						}
					}
					catch (Exception ex)
					{
						return new OperateResult<string>(ex.Message);
					}
				}
				result = this.ReadByAddress(address);
			}
			return result;
		}

		/// <summary>
		/// 使用POST的方式来向对方进行请求数据信息，需要使用url=开头，来表示是使用了原生的地址访问<br />
		/// Using POST to request data information from the other party, we need to start with url= to indicate that we are using native address access
		/// </summary>
		/// <param name="address">指定的地址信息，有些设备可能不支持</param>
		/// <param name="value">原始的字节数据信息</param>
		/// <returns>是否成功的写入</returns>
		// Token: 0x06001C2E RID: 7214 RVA: 0x0008B51C File Offset: 0x0008971C
		public virtual OperateResult Write(string address, byte[] value)
		{
			return this.Write(address, Encoding.Default.GetString(value));
		}

		/// <summary>
		/// 使用POST的方式来向对方进行请求数据信息，需要使用url=开头，来表示是使用了原生的地址访问<br />
		/// Using POST to request data information from the other party, we need to start with url= to indicate that we are using native address access
		/// </summary>
		/// <param name="address">指定的地址信息</param>
		/// <param name="value">字符串的数据信息</param>
		/// <returns>是否成功的写入</returns>
		// Token: 0x06001C2F RID: 7215 RVA: 0x0008B530 File Offset: 0x00089730
		public virtual OperateResult Write(string address, string value)
		{
			bool flag = address.StartsWith("url=") || address.StartsWith("URL=");
			if (flag)
			{
				address = address.Substring(4);
				string requestUri = string.Format("http://{0}:{1}/{2}", this.ipAddress, this.port, address.StartsWith("/") ? address.Substring(1) : address);
				try
				{
					using (StringContent stringContent = new StringContent(value))
					{
						using (HttpResponseMessage result = this.httpClient.PostAsync(requestUri, stringContent).Result)
						{
							using (HttpContent content = result.Content)
							{
								result.EnsureSuccessStatusCode();
								string result2 = content.ReadAsStringAsync().Result;
								return OperateResult.CreateSuccessResult<string>(result2);
							}
						}
					}
				}
				catch (Exception ex)
				{
					return new OperateResult<string>(ex.Message);
				}
			}
			return new OperateResult<string>(StringResources.Language.NotSupportedFunction);
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkWebApiBase.Read(System.String)" />
		// Token: 0x06001C30 RID: 7216 RVA: 0x0008B664 File Offset: 0x00089864
		[DebuggerStepThrough]
		public virtual Task<OperateResult<byte[]>> ReadAsync(string address)
		{
			NetworkWebApiBase.<ReadAsync>d__9 <ReadAsync>d__ = new NetworkWebApiBase.<ReadAsync>d__9();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<NetworkWebApiBase.<ReadAsync>d__9>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkWebApiBase.ReadString(System.String)" />
		// Token: 0x06001C31 RID: 7217 RVA: 0x0008B6B0 File Offset: 0x000898B0
		[DebuggerStepThrough]
		public virtual Task<OperateResult<string>> ReadStringAsync(string address)
		{
			NetworkWebApiBase.<ReadStringAsync>d__10 <ReadStringAsync>d__ = new NetworkWebApiBase.<ReadStringAsync>d__10();
			<ReadStringAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadStringAsync>d__.<>4__this = this;
			<ReadStringAsync>d__.address = address;
			<ReadStringAsync>d__.<>1__state = -1;
			<ReadStringAsync>d__.<>t__builder.Start<NetworkWebApiBase.<ReadStringAsync>d__10>(ref <ReadStringAsync>d__);
			return <ReadStringAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkWebApiBase.Write(System.String,System.Byte[])" />
		// Token: 0x06001C32 RID: 7218 RVA: 0x0008B6FC File Offset: 0x000898FC
		[DebuggerStepThrough]
		public virtual Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			NetworkWebApiBase.<WriteAsync>d__11 <WriteAsync>d__ = new NetworkWebApiBase.<WriteAsync>d__11();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<NetworkWebApiBase.<WriteAsync>d__11>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkWebApiBase.Write(System.String,System.String)" />
		// Token: 0x06001C33 RID: 7219 RVA: 0x0008B750 File Offset: 0x00089950
		[DebuggerStepThrough]
		public virtual Task<OperateResult> WriteAsync(string address, string value)
		{
			NetworkWebApiBase.<WriteAsync>d__12 <WriteAsync>d__ = new NetworkWebApiBase.<WriteAsync>d__12();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<NetworkWebApiBase.<WriteAsync>d__12>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 获取或设置远程服务器的IP地址<br />
		/// Gets or sets the IP address of the remote server
		/// </summary>
		// Token: 0x17000581 RID: 1409
		// (get) Token: 0x06001C34 RID: 7220 RVA: 0x0008B7A2 File Offset: 0x000899A2
		// (set) Token: 0x06001C35 RID: 7221 RVA: 0x0008B7AA File Offset: 0x000899AA
		public string IpAddress
		{
			get
			{
				return this.ipAddress;
			}
			set
			{
				this.ipAddress = value;
			}
		}

		/// <summary>
		/// 获取或设置远程服务器的端口号信息<br />
		/// Gets or sets the port number information for the remote server
		/// </summary>
		// Token: 0x17000582 RID: 1410
		// (get) Token: 0x06001C36 RID: 7222 RVA: 0x0008B7B3 File Offset: 0x000899B3
		// (set) Token: 0x06001C37 RID: 7223 RVA: 0x0008B7BB File Offset: 0x000899BB
		public int Port
		{
			get
			{
				return this.port;
			}
			set
			{
				this.port = value;
			}
		}

		/// <summary>
		/// 获取或设置当前的用户名<br />
		/// Get or set the current username
		/// </summary>
		// Token: 0x17000583 RID: 1411
		// (get) Token: 0x06001C38 RID: 7224 RVA: 0x0008B7C4 File Offset: 0x000899C4
		// (set) Token: 0x06001C39 RID: 7225 RVA: 0x0008B7CC File Offset: 0x000899CC
		public string UserName
		{
			get
			{
				return this.name;
			}
			set
			{
				this.name = value;
			}
		}

		/// <summary>
		/// 获取或设置当前的密码<br />
		/// Get or set the current password
		/// </summary>
		// Token: 0x17000584 RID: 1412
		// (get) Token: 0x06001C3A RID: 7226 RVA: 0x0008B7D5 File Offset: 0x000899D5
		// (set) Token: 0x06001C3B RID: 7227 RVA: 0x0008B7DD File Offset: 0x000899DD
		public string Password
		{
			get
			{
				return this.password;
			}
			set
			{
				this.password = value;
			}
		}

		/// <inheritdoc cref="P:HslCommunication.Core.Net.NetworkBase.LogNet" />
		// Token: 0x17000585 RID: 1413
		// (get) Token: 0x06001C3C RID: 7228 RVA: 0x0008B7E6 File Offset: 0x000899E6
		// (set) Token: 0x06001C3D RID: 7229 RVA: 0x0008B7EE File Offset: 0x000899EE
		public ILogNet LogNet { get; set; }

		/// <summary>
		/// 获取当前的HttpClinet的客户端
		/// </summary>
		// Token: 0x17000586 RID: 1414
		// (get) Token: 0x06001C3E RID: 7230 RVA: 0x0008B7F7 File Offset: 0x000899F7
		public HttpClient Client
		{
			get
			{
				return this.httpClient;
			}
		}

		/// <inheritdoc />
		// Token: 0x06001C3F RID: 7231 RVA: 0x0008B7FF File Offset: 0x000899FF
		public override string ToString()
		{
			return string.Format("NetworkWebApiBase[{0}:{1}]", this.ipAddress, this.port);
		}

		// Token: 0x04000615 RID: 1557
		private string ipAddress = "127.0.0.1";

		// Token: 0x04000616 RID: 1558
		private int port = 80;

		// Token: 0x04000617 RID: 1559
		private string name = string.Empty;

		// Token: 0x04000618 RID: 1560
		private string password = string.Empty;

		// Token: 0x04000619 RID: 1561
		private HttpClient httpClient;
	}
}
